"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : GUC参数--资源消耗
Case Name   : 修改参数shared_buffers为无效值
Create At   : 2020/11/17
Owner       : opentestcase037
Description :
              1、查看shared_buffers默认值；
              show shared_buffers;
              2、修改shared_buffers分别为15、1073741824、1024.25、
              'test'校验其预期结果；
Expect      :
              1、显示默认值为1GB；
              2、参数修改失败；
History     :
             modified：2021-04-08 by opentestcase005;修改用例,添加边界值校验
             modified：2022-05-18 by opentestcase040;修改用例,刪除shared_buffers參數默认值断言
"""

import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node

COMMONSH = CommonSH('PrimaryDbUser')


class GucTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.constant = Constant()
        self.log.info('==Opengauss_Function_Guc_Resource_Case0024开始==')
        self.db_user_node = Node(node='PrimaryDbUser')

    def test_startdb(self):
        self.log.info("查询该参数默认值")
        sql_cmd = COMMONSH.execut_db_sql('show shared_buffers;')
        self.log.info(sql_cmd)
        self.res = sql_cmd.splitlines()[-2].strip()
        self.log.info("修改shared_buffers为无效值")
        invalid_value = ['15', '1073741824', '1024.25', 'test']
        for i in invalid_value:
            result = COMMONSH.execute_gsguc('set',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            f"shared_buffers={i}")
            self.assertFalse(result)

    def tearDown(self):
        self.log.info("恢复默认值")
        sql_cmd = COMMONSH.execut_db_sql('show shared_buffers;')
        self.log.info(sql_cmd)
        msg = COMMONSH.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG,
                                     f'''shared_buffers={self.res}''')
        self.log.info(msg)
        msg = COMMONSH.restart_db_cluster()
        self.log.info(msg)
        status = COMMONSH.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)
        self.log.info('==Opengauss_Function_Guc_Resource_Case0024完成==')
